
//解析时间戳
function parseTimestamp(timestamp){
    let date = new Date(parseInt(timestamp));

    let Year = date.getFullYear();
    let Moth = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
    let Day = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate());
    let Hour = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours());
    let Minute = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes());
    let Second = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
    let  GMT =  Year + '-' + Moth + '-' + Day + ' '+ Hour +':'+ Minute  + ':' + Second;

    console.log(GMT)  // 2022-09-07 15:56:07
    return GMT;
}

//解析时间字符串，返回与当前时间戳的差值
function parseTimeStr(timeStr){
    let now=Date.now();

    let year = parseInt(timeStr.substring(0, 4));
    let month = parseInt(timeStr.substring(5, 7));
    let day = parseInt(timeStr.substring(8, 10));
    let hours = parseInt(timeStr.substring(11, 13));
    let minutes = parseInt(timeStr.substring(14, 16));
    let seconds = parseInt(timeStr.substring(17, 19));
    let newDate=new Date(year, month - 1, day, hours, minutes, seconds);
    let date=newDate.getTime();

    return now-date
}

//解析token
function parseToken(tokenstr){
    let arr=tokenstr.split("_");
    let userType=arr[0];
    let account=arr[1];
    let password=arr[2];
    return {
        userType,
        account,
        password
    };
}

//解析字符串
function parseTime(timeString){
    // const timeString = "2023-04-05T15:11:21";
    const dateObj = new Date(timeString);

    const year = dateObj.getFullYear();
    const month = dateObj.getMonth() + 1; // 月份从0开始，需要加1
    const day = dateObj.getDate();
    const hour = dateObj.getHours();
    const minute = dateObj.getMinutes();
    const second = dateObj.getSeconds();

    return `${year}-${month}-${day} ${hour}:${minute}:${second}`;
}

let util={
    parseToken,
    parseTime,
    parseTimestamp,
    parseTimeStr
};
export default util
